package com.example.backend.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.backend.entity.User;
import com.example.backend.entity.UserRole;
import com.example.backend.mapper.RoleMapper;
import com.example.backend.mapper.UserMapper;
import com.example.backend.mapper.UserRoleMapper;
import com.example.backend.service.UserService;
import com.example.backend.util.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户信息相关服务实现
 * @author 李月
 * @date 2022/4/13 20:57
 */
@Component
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public int register(String phoneNumber, String password) {
        User user = new User();
        UserRole userRole = new UserRole();
        QueryWrapper<User> wrapper1 = new QueryWrapper<User>();
        wrapper1.eq("phone_number", phoneNumber);
        if(userMapper.selectOne(wrapper1) != null){
            return 1;
        }
        if(!password.matches("^[0-9A-Za-z.\\-_]{7,}$")){
            return 2;
        }
        user.setPhone_number(phoneNumber);
        user.setPassword(password);
        userMapper.insert(user);

        QueryWrapper<User> wrapper2 = new QueryWrapper<User>();
        wrapper2.eq("phone_number", phoneNumber).eq("password", password);
        int userId = userMapper.selectOne(wrapper2).getUser_id();
        userRole.setUser_id(userId);
        userRole.setRole_id(2);
        userRoleMapper.insert(userRole);
        return 0;
    }

    @Override
    public String login(String phoneNumber, String password) {
        User user = new User();
        QueryWrapper<User> wrapper1 = new QueryWrapper<User>();
        QueryWrapper<User> wrapper2 = new QueryWrapper<User>();
        wrapper1.eq("phone_number", phoneNumber);
        wrapper2.eq("phone_number", phoneNumber).eq("password", password);
        if(userMapper.selectOne(wrapper1) == null){
            return "1";
        } else if(userMapper.selectOne(wrapper2) == null){
            return "2";
        } else{
            QueryWrapper<User> wrapper3 = new QueryWrapper<User>();
            wrapper3.eq("phone_number", phoneNumber).eq("password", password);
            user = userMapper.selectOne(wrapper3);
            String phone = user.getPhone_number();
            System.out.println(userMapper.selectOne(wrapper3).getPhone_number());
            int userId = user.getUser_id();
            return JWTUtil.createToken(userId) + ";" + userId;
        }
    }

    public int getRoleId(int userId){
        QueryWrapper<UserRole> wrapper = new QueryWrapper<UserRole>();
        wrapper.eq("user_id", userId);
        return userRoleMapper.selectOne(wrapper).getRole_id();
    }

    @Override
    public boolean modify(Map<String, Object> params) {
        int userId = (int) params.get("userId");
        User user = userMapper.selectById(userId);
        String nickname = (String) params.get("nickname");
        String sex = (String) params.get("sex");
        Date birthday = Date.valueOf((String) params.get("birthday"));
        String education = (String) params.get("education");
        String school = (String) params.get("school");
        String career = (String) params.get("career");
        user.setNickname(nickname);
        if(sex.equals("m")) {
            user.setSex("男");
        }
        else if(sex.equals("f")) {
            user.setSex("女");
        }
        else{
            user.setSex(sex);
        }
        user.setBirthday(birthday);
        user.setEducation(education);
        user.setSchool(school);
        user.setCareer(career);
        userMapper.updateById(user);

        User updateUser = userMapper.selectById(userId);

//        if(updateUser.getNickname().equals(nickname) && updateUser.getSex().equals(sex)
//            && updateUser.getBirthday().equals(birthday) && updateUser.getEducation().equals(education)
//            && updateUser.getSchool().equals(school) && updateUser.getCareer().equals(career)){
//            return true;
//        }
        return true;
    }

    @Override
    public Map<String, String> getInformation(int userId) {
        Map<String, String> userInfo = new HashMap<String, String>();
        User user = userMapper.selectById(userId);
        userInfo.put("nickname", user.getNickname());
        userInfo.put("sex", user.getSex());
        if(user.getBirthday()==null){
            userInfo.put("birthday", "");
        }
        else {
            userInfo.put("birthday", user.getBirthday().toString());
        }
        userInfo.put("education", user.getEducation());
        userInfo.put("school", user.getSchool());
        userInfo.put("career", user.getCareer());
        return userInfo;
    }
}
